// var app = angular.module('app', []);
app.controller('listallimgs', function($scope,$http,$rootScope){
    $http.get($rootScope.host+"/api/imgs/Select_all").success(function (response){$scope.phs=response.data;});
    $scope.updatedata=function(){
        $http.get($rootScope.host+"/api/imgs/Select_all").success(function (response){$scope.phs=response.data;});
    };
    $scope.updatefields=function(){
        $http.get($rootScope.host+"/api/imgs/update_fields/").success(function (response){
            var r=response.code;
            var t=response.message;
            if (r!='0'){
                alert(t);
                return;
            }
            $scope.updatedata();
            alert('update ok!');
        }).error(function(response){
            alert('update BOOM!!!!!!');
        });
    };
    $scope.uploadimgs=function(){
        var fd=new FormData();
        var file=document.getElementById('upimage').files[0];
        var str = document.getElementById("upimage").value;
        if(str.length==0){
            alert("您未选择任何图片!");
            return;
        }
        fd.append('ImgID','FFF');
        fd.append('UpImage',file);
        $http({
            method:'POST',
            url:$rootScope.host+"/api/imgs/?",
            data:fd,
            headers:{ 'Content-Type':undefined },
            transformRequest:angular.identity
        }).success(function(data,header,config,status){
            alert('upload ok');
            $scope.updatefields();
        }).error(function(){
            alert('upload BOOM!!!!!!');
        });
    };
    $scope.upsingle=function(file){
        var fd=new FormData();
        fd.append('ImgID','FFF');
        fd.append('UpImage',file);
        $http({
            method:'POST',
            url:$rootScope.host+"/api/imgs/?",
            data:fd,
            headers:{ 'Content-Type':undefined },
            transformRequest:angular.identity
        }).success(function(data,header,config,status){
            $scope.updatefields();
        }).error(function(){

        });
    };
    $scope.uploadpics=function(file){
        var filelist=document.getElementById('upimages').files;
        var str = document.getElementById("upimages").value;
        if(str.length==0){
            alert("您未选择任何图片!");
            return;
        }
        var flen=filelist.length;
        for (var i = 0; i < flen; i++){
            $scope.upsingle(filelist[i]);
        }
    };
});
app.controller('selectcategory', function($scope,$http,$stateParams,$rootScope){
    var cty=$stateParams.Category;
    var path=$rootScope.host+"/api/imgs/Select_Fields/?Category="+cty;
    $http.get(path).success(function (response){$scope.phs=response.data;});
});
app.controller('highscoreimgs', function($scope,$http,$rootScope,$interval){
    $scope.flag=0;
    $scope.TakeTimeSecond = 0;
    //Timer!!!!!!!!!!!!!!!!
    var stopEvent;
    $scope.startEvent = function(){
        if(angular.isDefined(stopEvent)) return;
        $scope.flag=1;
        stopEvent = $interval(function(){
            $scope.TakeTimeSecond=$scope.TakeTimeSecond+1;
        },1000)
    };
    $scope.stopClick = function(){
        if(angular.isDefined(stopEvent))
        {
            $interval.cancel(stopEvent);
            stopEvent = undefined;
        }
    };
    $scope.resetEvent = function(){
        $scope.count = 0;
        $scope.flag=1;
    };
    $scope.$on("$destory",function(){
        $scope.stopClick();
    });
    //Timer!!!!!!!!!!!!!!!!!!!!!!!!
    $scope.resetEvent();
    $scope.startEvent();
    $http.get($rootScope.host+"/api/imgs/GetHighScoreImgs").success(function (response){
        $scope.evaphs=response.data;
        $scope.stopClick();
    });
});
app.controller('similarpics', function($scope,$http,$rootScope,$interval){
    $scope.flag=0;
    $scope.TakeTimeSecond = 0;
    //Timer!!!!!!!!!!!!!!!!
    var stopEvent;
    $scope.startEvent = function(){
        if(angular.isDefined(stopEvent)) return;
        $scope.flag=1;
        stopEvent = $interval(function(){
            $scope.TakeTimeSecond=$scope.TakeTimeSecond+1;
        },1000)
    };
    $scope.stopClick = function(){
        if(angular.isDefined(stopEvent))
        {
            $interval.cancel(stopEvent);
            stopEvent = undefined;
        }
    };
    $scope.resetEvent = function(){
        $scope.count = 0;
        $scope.flag=1;
    };
    $scope.$on("$destory",function(){
        $scope.stopClick();
    });
    //Timer!!!!!!!!!!!!!!!!!!!!!!!!
    $scope.resetEvent();
    $scope.startEvent();
    $http.get($rootScope.host+"/api/imgs/Find_Similar").success(function (response){
        $scope.groups=response.data;
        $scope.stopClick();
    });
});
app.controller('timeaxis', function($scope,$http,$rootScope){
    $http.get($rootScope.host+"/api/imgs/TimeAxis").success(function (response){$scope.groups=response.data;});
});
app.controller('browseimgs', function($scope,$http,$rootScope){
    var mylabels=["动物","植物","食物","建筑物","工具及用品","景观","其它"];
    $scope.choices=[
        {code:'0',content:"动物"},
        {code:'1',content:"植物"},
        {code:'2',content:"食物"},
        {code:'3',content:"建筑物"},
        {code:'4',content:"工具及用品"},
        {code:'5',content:"景观"},
        {code:'6',content:"其它"}
    ];
    $scope.i=0;
    $scope.imclass="动物";
    $http.get($rootScope.host+"/api/imgs/Select_all").success(function (response){
        $scope.phs=response.data;
        $scope.length=$scope.phs.length;
        var tmp1=$scope.phs[$scope.i].NewCategory;
        tmp=parseInt(tmp1);
        $scope.imclass=mylabels[tmp];
    });
    $scope.initial=function(){
        $http.get($rootScope.host+"/api/imgs/Select_all").success(function (response){
            $scope.phs=response.data;
            $scope.length=$scope.phs.length;
            var tmp1=$scope.phs[$scope.i].NewCategory;
            tmp=parseInt(tmp1);
            $scope.imclass=mylabels[tmp];
        });
        $scope.i=0;
    };
    $scope.init=function(){
        $http.get($rootScope.host+"/api/imgs/Select_all").success(function (response){
            $scope.phs=response.data;
            $scope.length=$scope.phs.length;
            var tmp1=$scope.phs[$scope.i].NewCategory;
            tmp=parseInt(tmp1);
            $scope.imclass=mylabels[tmp];
        });
        $scope.i=$scope.i;
    };
    $scope.flush=function(){
        $http.get($rootScope.host+"/api/imgs/Select_all").success(function (response){
            $scope.phs=response.data;
            $scope.length=$scope.phs.length;
            var tmp1=$scope.phs[$scope.i].NewCategory;
            tmp=parseInt(tmp1);
            $scope.imclass=mylabels[tmp];
        });
        if($scope.i>=$scope.length-1){
            $scope.i=0;
        }
        else{
            $scope.i=$scope.i;
        }
        // $scope.i=0;
    };
    $scope.browsenext=function(){
        if ($scope.i >=$scope.length-1){
            $scope.i=0;
        }
        else{
            $scope.i=$scope.i+1;
        }
        var tmp1=$scope.phs[$scope.i].NewCategory;
        tmp=parseInt(tmp1);
        $scope.imclass=mylabels[tmp];
    };
    $scope.browselast=function(){
        if ($scope.i <= 0){
            $scope.i=$scope.length-1;
        }
        else{
            $scope.i=$scope.i-1;
        }
        var tmp1=$scope.phs[$scope.i].NewCategory;
        tmp=parseInt(tmp1);
        $scope.imclass=mylabels[tmp];
    };
    $scope.PushDescription=function(){
        var imid=$scope.phs[$scope.i].id;
        var pushurl=$rootScope.host+"/api/imgs/"+imid+"/partset?txt="+$scope.des;
        $http.get(pushurl).success(function (response){
            $scope.init();
            alert('push ok!');
        }).error(function(response){
            alert('push failed...');
        });
    };
    $scope.PushCategory=function(){
        var imid=$scope.phs[$scope.i].id;
        var pushurl=$rootScope.host+"/api/imgs/"+imid+"/partset?category="+$scope.seleteclass;
        $http.get(pushurl).success(function (response){
            $scope.init();
            alert('push ok!');
        }).error(function(response){
            alert('push failed...');
        });
    };
    $scope.DelImg=function(){
        var imid=$scope.phs[$scope.i].id;
        $http({
        method:'get',
        url:$rootScope.host+'/api/imgs/DeleteImgs?id='+imid
      }).success(function(){
          $scope.flush();
          alert('delwell!');
        }).error(function(){
            alert('DelBOOM!!!!');
        });
      $http.get($rootScope.host+"/api/imgs/Select_all").success(function (response){
            $scope.phs=response.data;
            $scope.length=$scope.phs.length;
            var tmp1=$scope.phs[$scope.i].NewCategory;
            tmp=parseInt(tmp1);
            $scope.imclass=mylabels[tmp];
        });
    };
});
app.controller('imgstransfer', function($scope,$http,$rootScope,$interval){
    $scope.styles=[
        {code:'1',content:"抽象风格"},
        {code:'2',content:"梦幻风格"},
        {code:'3',content:"狂野风格"},
        {code:'4',content:"静谧风格"},
        {code:'5',content:"朦胧风格"},
    ];
    $scope.i=0;
    $scope.styleurl=null;
    $scope.flag=0;
    $scope.TakeTimeSecond = 0;
    $http.get($rootScope.host+"/api/imgs/Select_all").success(function (response){
        $scope.phs=response.data;
        $scope.length=$scope.phs.length;
    });
    $scope.initial=function(){
        $http.get($rootScope.host+"/api/imgs/Select_all").success(function (response){
            $scope.phs=response.data;
            $scope.length=$scope.phs.length;
        });
        $scope.i=0;
        $scope.flag=0;
        $scope.TakeTimeSecond = 0;
    };
    $scope.browsenext=function(){
        if ($scope.i >=$scope.length-1){
            $scope.i=0;
        }
        else{
            $scope.i=$scope.i+1;
        }
    };
    $scope.browselast=function(){
        if ($scope.i <= 0){
            $scope.i=$scope.length-1;
        }
        else{
            $scope.i=$scope.i-1;
        }
    };
    //Timer!!!!!!!!!!!!!!!!
    var stopEvent;
    $scope.startEvent = function(){
        if(angular.isDefined(stopEvent)) return;
        $scope.flag=1;
        stopEvent = $interval(function(){
            $scope.TakeTimeSecond=$scope.TakeTimeSecond+1;
        },1000)
    };
    $scope.stopClick = function(){
        if(angular.isDefined(stopEvent))
        {
            $interval.cancel(stopEvent);
            stopEvent = undefined;
        }
    };
    $scope.resetEvent = function(){
        $scope.count = 0;
        $scope.flag=1;
    };
    $scope.$on("$destory",function(){
        $scope.stopClick();
    });
    //Timer!!!!!!!!!!!!!!!!!!!!!!!!
    $scope.transefer=function(){
        var imid=$scope.phs[$scope.i].id;
        var pushurl=$rootScope.host+"/api/imgs/"+imid+"/transferstyle?style="+$scope.seletestyles;
        $scope.resetEvent();
        $scope.startEvent();
        $http.get(pushurl).success(function (response){
            $scope.styleurl=response.data;
            alert('Transfer ok!');
            $scope.stopClick();
        }).error(function(response){alert('Transfer failed...')});
    };
});
app.controller('searchfaces', function($scope,$http,$rootScope,$interval){
    $scope.i=0;
    $scope.flag=0;
    $scope.TakeTimeSecond = 0;
    $http.get($rootScope.host+"/api/imgs/Select_all").success(function (response){
        $scope.phs=response.data;
        $scope.length=$scope.phs.length;
    });
    $scope.initial=function(){
        $http.get($rootScope.host+"/api/imgs/Select_all").success(function (response){
            $scope.phs=response.data;
            $scope.length=$scope.phs.length;
        });
        $scope.i=0;
        $scope.flag=0;
        $scope.TakeTimeSecond = 0;
    };
    $scope.browsenext=function(){
        if ($scope.i >=$scope.length-1){
            $scope.i=0;
        }
        else{
            $scope.i=$scope.i+1;
        }
        $scope.flag=0;
        $scope.TakeTimeSecond = 0;
    };
    $scope.browselast=function(){
        if ($scope.i <= 0){
            $scope.i=$scope.length-1;
        }
        else{
            $scope.i=$scope.i-1;
        }
        $scope.flag=0;
        $scope.TakeTimeSecond = 0;
    };
    //Timer!!!!!!!!!!!!!!!!
    var stopEvent;
    $scope.startEvent = function(){
        if(angular.isDefined(stopEvent)) return;
        $scope.flag=1;
        stopEvent = $interval(function(){
            $scope.TakeTimeSecond=$scope.TakeTimeSecond+1;
        },1000)
    };
    $scope.stopClick = function(){
        if(angular.isDefined(stopEvent))
        {
            $interval.cancel(stopEvent);
            stopEvent = undefined;
        }
    };
    $scope.resetEvent = function(){
        $scope.count = 0;
        $scope.flag=1;
    };
    $scope.$on("$destory",function(){
        $scope.stopClick();
    });
    //Timer!!!!!!!!!!!!!!!!!!!!!!!!
    $scope.search=function(){
        var imid=$scope.phs[$scope.i].id;
        var SearchUrl=$rootScope.host+'/api/imgs/SearchFace?id='+imid;
        $scope.resetEvent();
        $scope.startEvent();
        $http.get(SearchUrl).success(function (response){
            $scope.faces=response.data;
            $scope.feedback=response.message;
            $scope.stopClick();
        });
    };
});
app.controller('FacesAgeGender', function($scope,$http,$rootScope,$interval){
    $scope.i=0;
    $scope.info=null;
    $scope.feedback=null;
    $scope.code=null;
    $scope.flag=0;
    $scope.TakeTimeSecond = 0;
    $http.get($rootScope.host+"/api/imgs/Select_all").success(function (response){
        $scope.phs=response.data;
        $scope.length=$scope.phs.length;
        $scope.info=null;
        $scope.feedback=null;
        $scope.code=null;
    });
    $scope.initial=function(){
        $http.get($rootScope.host+"/api/imgs/Select_all").success(function (response){
            $scope.phs=response.data;
            $scope.length=$scope.phs.length;
        });
        $scope.i=0;
        $scope.info=null;
        $scope.feedback=null;
        $scope.code=null;
        $scope.flag=0;
        $scope.TakeTimeSecond = 0;
    };
    $scope.browsenext=function(){
        if ($scope.i >=$scope.length-1){
            $scope.i=0;
        }
        else{
            $scope.i=$scope.i+1;
        }
        $scope.info=null;
        $scope.feedback=null;
        $scope.code=null;
        $scope.flag=0;
        $scope.TakeTimeSecond = 0;
    };
    $scope.browselast=function(){
        if ($scope.i <= 0){
            $scope.i=$scope.length-1;
        }
        else{
            $scope.i=$scope.i-1;
        }
        $scope.info=null;
        $scope.feedback=null;
        $scope.code=null;
        $scope.flag=0;
        $scope.TakeTimeSecond = 0;
    };
    //Timer!!!!!!!!!!!!!!!!
    var stopEvent;
    $scope.startEvent = function(){
        if(angular.isDefined(stopEvent)) return;
        $scope.flag=1;
        stopEvent = $interval(function(){
            $scope.TakeTimeSecond=$scope.TakeTimeSecond+1;
        },1000)
    };
    $scope.stopClick = function(){
        if(angular.isDefined(stopEvent))
        {
            $interval.cancel(stopEvent);
            stopEvent = undefined;
        }
    };
    $scope.resetEvent = function(){
        $scope.count = 0;
        $scope.flag=1;
    };
    $scope.$on("$destory",function(){
        $scope.stopClick();
    });
    //Timer!!!!!!!!!!!!!!!!!!!!!!!!
    $scope.prediction=function(){
        var imid=$scope.phs[$scope.i].id;
        var PreUrl=$rootScope.host+'/api/imgs/Judge_Age_Gender?id='+imid;
        $scope.resetEvent();
        $scope.startEvent();
        $http.get(PreUrl).success(function (response){
            $scope.info=response.data;
            $scope.feedback=response.message;
            $scope.code=response.code;
            $scope.stopClick();
        });
    };
});
app.controller('FacesSmile', function($scope,$http,$rootScope,$interval){
    $scope.flag=0;
    $scope.TakeTimeSecond = 0;
    //Timer!!!!!!!!!!!!!!!!
    var stopEvent;
    $scope.startEvent = function(){
        if(angular.isDefined(stopEvent)) return;
        $scope.flag=1;
        stopEvent = $interval(function(){
            $scope.TakeTimeSecond=$scope.TakeTimeSecond+1;
        },1000)
    };
    $scope.stopClick = function(){
        if(angular.isDefined(stopEvent))
        {
            $interval.cancel(stopEvent);
            stopEvent = undefined;
        }
    };
    $scope.resetEvent = function(){
        $scope.count = 0;
        $scope.flag=1;
    };
    $scope.$on("$destory",function(){
        $scope.stopClick();
    });
    //Timer!!!!!!!!!!!!!!!!!!!!!!!!
    $scope.resetEvent();
    $scope.startEvent();
    $http.get($rootScope.host+"/api/imgs/Select_Smile").success(function (response){
        $scope.phs=response.data;
        $scope.stopClick();
    });
});